//Copyright 2008 by Wes Corp for Trafficman  All rights reserved.abstract 

package org.trafficman.trafficman_main;

//import org.trafficman.trafficman_main.Contact_Roles_NoteEdit.ImageAdapter;



import java.text.SimpleDateFormat;
import java.util.Date;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.database.Cursor;
import android.graphics.drawable.Drawable;
import android.os.Bundle;
import android.util.DateFormat;
import android.util.Time;
import android.view.View;
import android.view.ViewGroup;
import android.view.Window;
import android.widget.AdapterView;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Gallery;
import android.widget.GridView;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.Gallery.LayoutParams;
import org.trafficman.trafficman_main.TrafficmanScreens;

public class TrafficmanFilenameNoteEdit extends Activity {

	private EditText mTitleText;
	private EditText mBodyText;
	private Long mRowId;
	private TrafficmanFilesNotesDbAdapter mDbHelper;
	private int viewWidth;
	private int viewHeight;
	private int int_intent_active;
	private int jv_selected_file_id;
	private int int_selected_id;
	private CharSequence str_jv_now;
	private String datetime_record_created;
	private String datetime_record_updated;
	private String title_text;
	private String jv_filename;

	@Override
	protected void onCreate(Bundle icicle) {
		super.onCreate(icicle);

		Window w = getWindow();
		w.requestFeature(Window.FEATURE_LEFT_ICON);

		setContentView(R.layout.files_note_edit_edit);

		// get the xml layout page
		LinearLayout tmpLayout = (LinearLayout) findViewById(R.id.linearlayout_001);
		TrafficmanConstants.SCREEN_VIEWWIDTH = this.getResources().getDisplayMetrics().widthPixels;
		TrafficmanConstants.SCREEN_VIEWHEIGHT = this.getResources().getDisplayMetrics().heightPixels;
		get_JobView_Screen(); // set JobView2 background
		int resID = getResources().getIdentifier(TrafficmanConstants.SCREEN_NAME, null,
				null);
		tmpLayout.setBackground(resID);

		mTitleText = (EditText) findViewById(R.id.title);
		//mBodyText = (EditText) findViewById(R.id.body);

		Button confirmButton = (Button) findViewById(R.id.confirm);
		Button cancelButton = (Button) findViewById(R.id.cancel);

		mRowId = icicle != null ? icicle
				.getLong(TrafficmanFilesNotesDbAdapter.KEY_ROWID) : null;
		if (mRowId == null) {
			Bundle extras = getIntent().getExtras();
			mRowId = extras != null ? extras
					.getLong(TrafficmanFilesNotesDbAdapter.KEY_ROWID) : null;
		}

		populateFields();

		confirmButton.setOnClickListener(new View.OnClickListener() {
			public void onClick(View view) {
				int_intent_active = 1;
				TrafficmanConstants.SAVE_CLICK = 1;
				setResult(RESULT_OK);
				Intent intent = new Intent();
				intent.setClass(TrafficmanFilenameNoteEdit.this,
						TrafficmanMainActivity.class);
				startActivity(intent);
				finish();
			}
		});

		cancelButton.setOnClickListener(new View.OnClickListener() {
			public void onClick(View view) {
				setResult(RESULT_CANCELED);
				Intent intent = new Intent();
				intent.setClass(TrafficmanFilenameNoteEdit.this,
						TrafficmanMainActivity.class);
				startActivity(intent);
				finish();
			}
		});
		
		title_text = getString(R.string.trafficman_file_name_editor);
		this.setTitle(title_text);
		w.setFeatureDrawableResource(Window.FEATURE_LEFT_ICON,
				R.drawable.jv_icon);
	}

	private void get_JobView_Screen() {
		TrafficmanScreens mScreens = new TrafficmanScreens(this);
	}

	private void populateFields() {
		if (mRowId != null) {
			mDbHelper = new TrafficmanFilesNotesDbAdapter(this);
			mDbHelper.open();

			Cursor note = mDbHelper.fetchNote(mRowId);
			startManagingCursor(note);
			mTitleText
					.setText(note
							.getString(note
									.getColumnIndex(TrafficmanFilesNotesDbAdapter.KEY_JV_TITLE)));
			mDbHelper.close();
		}
	}

	@Override
	protected void onFreeze(Bundle outState) {
		super.onFreeze(outState);
		outState.putLong(TrafficmanFilesNotesDbAdapter.KEY_ROWID, mRowId);
	}

	@Override
	protected void onPause() {
		if (int_intent_active == 1) {
			// do nothing
			int_intent_active = 0;
			super.onPause();
			saveState();
		} else {
			super.onPause();
			// Intent intent = new Intent();
			// intent.setClass(JobView_Files_NoteEdit.this,
			// MainMenu_Notepad.class);
			// startActivity(intent);
			finish();
		}
	}

	@Override
	protected void onResume() {
		super.onResume();
		populateFields();
	}

	private void saveState() {
//		if (Constants.CANCEL_CLICK == 1) {
//			// do nothing
//			Constants.CANCEL_CLICK = 0;
//		} else {
			if (TrafficmanConstants.SAVE_CLICK == 1) {

				String title = mTitleText.getText().toString();
				jv_selected_file_id = 0;
				if (title.length() > 0) {
					mDbHelper = new TrafficmanFilesNotesDbAdapter(this);
					mDbHelper.open();
					
					TrafficmanDateStamp dateStamp = new TrafficmanDateStamp();
					datetime_record_updated = dateStamp.datetime_now.toString();
					jv_filename = TrafficmanConstants.CURRENT_JV_FILENAME;
					
					if (mRowId == null) {
						datetime_record_created = datetime_record_updated;
						long id = mDbHelper.createNote(title,
								jv_selected_file_id, datetime_record_created, datetime_record_updated, jv_filename);
						if (id > 0) {
							mRowId = id;
						}
					} else {
						datetime_record_created = TrafficmanConstants.DATETIME_RECORD_CREATED;
						mDbHelper
								.updateNote(mRowId, title, jv_selected_file_id, datetime_record_created, datetime_record_updated, jv_filename);
						TrafficmanConstants.DATETIME_RECORD_CREATED = "";
					}
					mDbHelper.close();
					//get_Current_JV_file();
				}
				TrafficmanConstants.SAVE_CLICK = 0;
			} else {
				// do nothing
			}
		}

//	}			jv_filename = Constants.CURRENT_JV_FILENAME;



	public class ImageAdapter extends BaseAdapter {

		private Integer[] mThumbIds = { R.drawable.projects_001,
				R.drawable.tasks_003, R.drawable.schedule_01,
				R.drawable.bidding_001, R.drawable.contacts_01 };

		public ImageAdapter(Context c) {
			mContext = c;
		}

		public int getCount() {
			return mThumbIds.length;
		}

		public Object getItem(int position) {
			return position;
		}

		public long getItemId(int position) {
			return position;
		}

		public View getView(int position, View convertView, ViewGroup parent) {
			ImageView i = new ImageView(mContext);

			i.setImageResource(mThumbIds[position]);
			i.setAdjustViewBounds(true);
			i.setLayoutParams(new Gallery.LayoutParams(
					LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT));
			i.setBackground(android.R.drawable.picture_frame);
			return i;
		}

		private Context mContext;

	}

	
	private void get_Current_JV_file() {

		TrafficmanFilesNotesDbAdapter current_JV_File_mDbHelper = new TrafficmanFilesNotesDbAdapter(
				this);
		current_JV_File_mDbHelper.open();

		Cursor current_JV_File_Cursor = current_JV_File_mDbHelper
				.fetchAllNotes();
		startManagingCursor(current_JV_File_Cursor);
		int record_count = current_JV_File_Cursor.count();
		int columnID = current_JV_File_Cursor.getColumnIndex("_id");
		int columnID2 = current_JV_File_Cursor.getColumnIndex("jv_title");
		int columnID3 = current_JV_File_Cursor
				.getColumnIndex("jv_selected_file_id");
		if (record_count > 0) {
			current_JV_File_Cursor.first();
			for (int i = 0; i < record_count; ++i) {
				int_selected_id = current_JV_File_Cursor.getInt(columnID3);
				if (int_selected_id == 1) {
					// we have a winner
					long lng_rowID = current_JV_File_Cursor.getLong(columnID);
					TrafficmanConstants.CURRENT_JV_FILE_ID = lng_rowID;
					TrafficmanConstants.CURRENT_JV_FILE_TITLE = current_JV_File_Cursor
							.getString(columnID2);
				}
				current_JV_File_Cursor.next();
			}
		}
	}

}
